package org.linlinjava.litemall.admin.controller;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import javax.validation.constraints.NotNull;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.beans.vo.RegionVo;
import org.linlinjava.litemall.admin.service.AdminRegionService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallRegion;
import org.linlinjava.litemall.db.service.LitemallRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理区域控制器
 *
 * @author yliyun
 * @date 2022/09/14
 */
@Api(tags = "管理区域API")
@RestController
@RequestMapping("/admin/region")
@Validated
public class AdminRegionController {

    /**
     * 地区服务
     */
    @Autowired
    private LitemallRegionService regionService;
    /**
     * 管理区域服务
     */
    @Autowired
    private AdminRegionService adminRegionService;

    /**
     * clist
     * 国家列表
     *
     * @return {@link Object}
     */
    @GetMapping("/clist")
    @LogAnno
    public Object clist() {
        List<LitemallRegion> countryList = regionService.queryByPid(0);
        return ResponseUtil.okList(countryList);
    }

    /**
     * plist
     * 子列表
     *
     * @param type {2:省份，3:城市}
     * @param id   id
     * @return {@link Object}
     */
    @ApiOperation("获取所有省份")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id"),
            @ApiImplicitParam(name = "type",value = "type")
    })
    @GetMapping("/slist")
    @LogAnno
    public Object plist(@RequestParam(value = "id") Integer id, @NotNull Integer type) {
        if(id == null){
            //获取所有省份
            return ResponseUtil.okList(regionService.queryByType(type.byteValue()));
        }else{
            List<LitemallRegion> regionList = regionService.queryByPid(id);
            return ResponseUtil.okList(regionList);
        }
    }

    /**
     * 位置
     * 地址，国家--省份--城市
     *
     * @return {@link Object}
     */
    @GetMapping("/locations")
    @LogAnno
    public Object locations() {
        List<LitemallRegion> countryList = regionService.queryByPid(0);

        //查询的所有的国家
        List<RegionVo> countryVoList = countryList.stream().map(country -> {
            return getRegionVo(country);
        }).collect(Collectors.toList());

        //循环国家找到对应的省份加入到children
        countryVoList = countryVoList.stream().map(country -> {
            List<LitemallRegion> provinceList = regionService.queryByPid(country.getId());
            List<RegionVo> provinceVos = new ArrayList<>();

            //循环根据国家id查询到的省份集合,封装成RegionVo
            provinceVos = provinceList.stream().map(province -> {
                List<LitemallRegion> cityList = regionService.queryByPid(province.getId());

                //循环根据省份id查询到的城市集合,封装成RegionVo
                List<RegionVo> cityVoList = cityList.stream().map(city -> {
                    return getRegionVo(city);
                }).collect(Collectors.toList());
                RegionVo vo = getRegionVo(province);
                        vo.setChildren(cityVoList);
                return vo;
            }).collect(Collectors.toList());

            country.setChildren(provinceVos);
            return country;
        }).collect(Collectors.toList());
        return ResponseUtil.okList(countryVoList);
    }

    /**
     * 列表
     *
     * @return {@link Object}
     */
    @GetMapping("/list")
    @LogAnno
    public Object list() {
        List<RegionVo> regionVoList = new ArrayList<>();

        List<LitemallRegion> countryList = regionService.queryByPid(0);
        for (LitemallRegion country : countryList) {
            RegionVo countryVO = new RegionVo();
            countryVO.setId(country.getId());
            countryVO.setNameCn(country.getNameCn());
            countryVO.setNameEn(country.getNameEn());
            countryVO.setCode(country.getCode());
            countryVO.setType(country.getType());

            List<LitemallRegion> provinceList = regionService.queryByPid(country.getId());
            List<RegionVo> provinceVOList = new ArrayList<>();
            for (LitemallRegion province : provinceList){
                RegionVo provinceVO = new RegionVo();
                provinceVO.setId(province.getId());
                provinceVO.setNameCn(province.getNameCn());
                provinceVO.setNameEn(province.getNameEn());
                provinceVO.setCode(province.getCode());
                provinceVO.setType(province.getType());

                List<LitemallRegion> cityList = regionService.queryByPid(province.getId());
            	List<RegionVo> cityVOList = new ArrayList<>();
                for (LitemallRegion city : cityList){
                    RegionVo cityVO = new RegionVo();
                    cityVO.setId(city.getId());
                    cityVO.setNameCn(city.getNameCn());
                    cityVO.setNameEn(city.getNameEn());
                    cityVO.setCode(city.getCode());
                    cityVO.setType(city.getType());
                    cityVOList.add(cityVO);
                }

                provinceVO.setChildren(cityVOList);
                provinceVOList.add(provinceVO);
            }

            countryVO.setChildren(provinceVOList);
            regionVoList.add(countryVO);
        }

        return ResponseUtil.ok(regionVoList);
    }

    /**
     * 父母
     * 获取所有父级id
     *
     * @param id id
     * @return {@link Object}
     */
    @GetMapping("/parents/{id}")
    @LogAnno
    public Object parents(@PathVariable Integer id) {
        LinkedList<Integer> list = new LinkedList<>();
        adminRegionService.getParentIds(id, list);
        return ResponseUtil.ok(list);
    }

    /**
     * 得到地区签证官
     *
     * @param region 地区
     * @return {@link RegionVo}
     */
    public RegionVo getRegionVo(LitemallRegion region){
        RegionVo regionVo = new RegionVo();
        regionVo.setId(region.getId());
        regionVo.setCode(region.getCode());
        regionVo.setNameCn(region.getNameCn());
        regionVo.setNameEn(region.getNameEn());
        regionVo.setType(region.getType());
        regionVo.setPid(region.getPid());
        return regionVo;
    }
}
